/*	
 *  Copyright (c) 2009-@year@. The GUITAR group at the University of Maryland. Names of owners of this group may
 *  be obtained by sending an e-mail to atif@cs.umd.edu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 *  documentation files (the "Software"), to deal in the Software without restriction, including without 
 *  limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 *	the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 
 *	conditions:
 * 
 *	The above copyright notice and this permission notice shall be included in all copies or substantial 
 *	portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
 *	LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO 
 *	EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 *	IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 
 *	THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */
package edu.umd.cs.imut.executor.replayer;

import java.util.LinkedList;

import org.apache.log4j.Logger;

import edu.umd.cs.imut.event.IEvent;
import edu.umd.cs.imut.exception.IException;
import edu.umd.cs.imut.executor.IExecutor;
import edu.umd.cs.imut.executor.matching.IDataMatchingCriteria;
import edu.umd.cs.imut.model.IApplication;
import edu.umd.cs.imut.model.IComponent;
import edu.umd.cs.imut.model.IIDGenerator;
import edu.umd.cs.imut.model.data.ComponentType;
import edu.umd.cs.imut.model.data.EventType;
import edu.umd.cs.imut.model.data.GUIMap;
import edu.umd.cs.imut.model.data.StepType;
import edu.umd.cs.imut.model.data.TestCase;
import edu.umd.cs.imut.model.wapper.GUIMapWrapper;

/**
 * 
 * @author <a href="mailto:baonn@cs.umd.edu"> Bao N. Nguyen </a>
 * 
 */
public abstract class IReplayer extends IExecutor {
	private final static Logger LOGGER = Logger
			.getLogger(IReplayer.class);

	GUIMap map;
	TestCase testcase;
	LinkedList<StepType> stepToExecute;
	IIDGenerator idGenerator;

	// ComponentDataCriteria componentIdentifer;
	IDataMatchingCriteria componentMatchingCriteria;

	/**
	 * @return the idGenerator
	 */
	public IIDGenerator getIdGenerator() {
		return idGenerator;
	}

	/**
	 * @param idGenerator
	 *            the idGenerator to set
	 */
	public void setIdGenerator(IIDGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	/**
	 * @param application
	 *            the application to replay
	 */
	public IReplayer(IApplication application) {
		super(application);
	}

	/**
	 * @param application
	 * @param testcase
	 * @param map
	 */
	public IReplayer(IApplication application, GUIMap map, TestCase testcase) {
		super(application);
		this.map = map;
		this.testcase = testcase;

		// Initialize the event list
		stepToExecute = new LinkedList<StepType>();

		for (StepType step : testcase.getStep()) {
			stepToExecute.addLast(step);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.umd.imut.executor.IExecutor#hasNextEvent()
	 */
	@Override
	public boolean hasNextEvent() {
		return (!stepToExecute.isEmpty());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.cs.umd.imut.executor.IExecutor#performNextEvent()
	 */
	@Override
	public void performNextEvent() throws IException {
		// // get next step
		StepType step = stepToExecute.poll();
		String eventID = step.getEventID();

		// lookup for the event
		GUIMapWrapper wMap = new GUIMapWrapper(map);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("EventID: " + eventID);
		}
		EventType eventData = wMap.getEventByID(eventID);

		if (eventData == null) {
			return;
		}

		String eventClassName = eventData.getAction();

		IEvent event = null;
		Class<?> eventClass;
		try {
			eventClass = Class.forName(eventClassName);
			event = (IEvent) eventClass.newInstance();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// lookup for the component
		LOGGER.info("Looking for component....");

		String componentID = eventData.getComponentID();

		ComponentType componentData = wMap.getComponentByID(componentID);
		componentMatchingCriteria.setComponentData(componentData);
		
		IComponent iComponent = searchComponent(componentData,
				componentMatchingCriteria);

		if (iComponent != null) {
			LOGGER.info("Component FOUND");
		} else {
			LOGGER.info("Component NOT found");
		}
		// perform event
		// event.perform(iComponent);

	}

}